package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.UserAddress;
import com.fx.service.UserAddressService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import java.util.List;
import java.util.Map;

/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:21
 */

@Controller
@RequestMapping("/userAddress")
public class UserAddressController extends BaseController {
    @Autowired
    private UserAddressService userAddressService;
    private static String pageTitle = "XXXX管理";
    private static String pageUrl = "/userAddress";

    @GetMapping()
    String userAddress(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "userAddress/userAddress";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<UserAddress> userAddressList = userAddressService.getList(query);
        int total = userAddressService.getCount(query);
        return renderPage(total, userAddressList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "userAddress/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        UserAddress userAddress = userAddressService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("userAddress", userAddress);
        return "userAddress/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(UserAddress userAddress) {
        String id = userAddress.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = userAddressService.save(userAddress);
        } else {
            result = userAddressService.updateById(userAddress);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (userAddressService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 方法名称：获取用户地址
     * 概要说明：获取用户地址
     *
     * @Ather EDZ
     * @Date 2018-6-25 14:26
     * @Param userId 用户id
     * @Return
     */
    @GetMapping("/api/getAddressByUserId")
    @ResponseBody
    public R getAddressByUserId(HttpServletRequest request) {
        String userId = String.valueOf(request.getAttribute("flId"));
        List<UserAddress> userAddressList = userAddressService.getListByUserId(userId);
        return R.ok(userAddressList);
    }

    /**
     * 方法名称：删除地址
     * 概要说明：根据地址id删除地址
     *
     * @Ather EDZ
     * @Date 2018-6-25 14:30
     * @Param addressId 地址id
     * @Return
     */
    @PostMapping("/api/removeAddress")
    @ResponseBody
    public R removeAddress(String addressId) {
        return R.ok(userAddressService.remove(addressId));
    }
    /**
     * 方法名称：获取地址详情
     * 概要说明：根据Id获取地址详情
     * @Ather EDZ
     * @Date 2018-6-25 14:35
     * @Param
     * @Return
     */
    @GetMapping("/api/getAddressById/{addressId}")
    @ResponseBody
    public R getAddressById(@PathVariable("addressId") String addressId) {
        return R.ok(userAddressService.getById(addressId));
    }

    /**
     * 方法名称：更新地址信息
     * 概要说明：更新地址信息
     * @Ather EDZ
     * @Date 2018-6-25 14:36
     * @Return
     */
    @PostMapping("/api/updateAddress")
    @ResponseBody
    public R updateAddress(UserAddress userAddress) {
        return R.ok(userAddressService.updateById(userAddress));
    }

    /**
     * 方法名称：修改默认地址
     * 概要说明：根据Id修改默认地址
     * @Ather EDZ
     * @Date 2018-6-25 14:35
     * @Param
     * @Return
     */
    @PostMapping("/api/changeDefaultAddress")
    @ResponseBody
    public R changeDefaultAddress(String addressId) {
        return R.ok(userAddressService.changeDefaultAddress(addressId));
    }
    /**
     * 方法名称：添加地址信息
     * 概要说明：添加地址信息
     * @Ather EDZ
     * @Date 2018-6-25 14:36
     * @Param
     * @Return
     */
    @PostMapping("/api/addOrEditAddress")
    @ResponseBody
    public R updateAddressById(HttpServletRequest request , UserAddress userAddress) {
        String userId = String.valueOf(request.getAttribute("flId"));
        userAddress.setFlUserId(userId);
        return R.ok(userAddressService.saveOrEdit(userAddress));
    }

}
